/**
 * Created by zyy on 15/5/30.
 * zhangyuyu@superjia.com
 */
(function ($) {
    var dir = {

        __path: '/',

        init: function (container, options) {
            var self = this
            self.__container = container
            self.__options = options
            self.__file = options.file

            self.__initList()

            self.__bindEvent()
        },

        __initList: function () {
            var self = this
            var container = self.__container
            var file = self.__file
            self.loadFiles(self.__path)

        },

        __bindEvent: function() {
            var self = this
            var container = self.__container

            container.on('click', '.back', function() {
                var path = $(this).data('path')
                $.jps.publish('file-list', {
                    path: path
                })
            })
        },

        loadFiles: function (path) {
            var self = this
            var file = self.__file

            var url = '/link/folder/' + file.linkid + '/'

            self.__path = path

            $.ajax({
                url: url,
                method: 'post',
                dataType: 'json',
                data: {
                    p: 0,
                    s: 1000,
                    path: path,
                    orderby: 'filename',
                    ordertype: 'asc'
                },
                success: function (data) {
                    if (data.code == 0) {
                        var files = data.data.subfiles
                        self.__container.empty()
                        if (files.length == 0) {
                            self.__showEmptyTip()
                            self.__initBack(data.data)
                            return false
                        }
                        _.each(files, function(item) {
                            var model = new Backbone.Model(item)
                            model.set('isLinkView', true)
                            model.set('linkid', file.linkid)
                            var view = new App.modules.docView({
                                model: model
                            })
                            self.__container.append(view.render().$el)
                        })
                        self.__initBack(data.data)
                    }
                }
            })
        },

        __initBack: function(file) {
            var self = this
            var path = self.__path
            var pathArr = path.split('/').slice(0, -2)
            var backPath = pathArr.join('/') + '/'
            $('.title').text(file.fileName)
            if(path != '/') {
                $(backTpl).prependTo(self.__container).data('path', backPath)
            }
        },
        __showEmptyTip: function () {
            var self = this
            var container = self.__container
            container.html(emptyTpl)
        }
    }

    var backTpl = '<div class="back">返回上一级</div>'
    var emptyTpl = '<div class="empty">空文件夹</div>'

    $.jps.subscribe('file-list', function (options) {
        var path = options.path
        dir.loadFiles(path)
    })

    //预览图片
    $.jps.subscribe('img-preview', function (file) {
        App.modules.preview.img(file)
    })

    //预览office
    $.jps.subscribe('office-preview', function (file) {
        App.modules.preview.office(file)
    })

    $(function () {
        dir.init($('.preview'), visitor)
    })
})(jQuery)